*** LOADING DATA FILE
cd "C:\Users\\`=c(username)'\Dropbox\02_ClassIDscandi\Data\BES" // replace with your directory
use replicationfile_Britain.dta, clear


set scheme s1mono // setting graphics style


*** ANALYSES 
cd "C:\Users\\`=c(username)'\02_ClassIDscandi\Outputs\Final figures" // replace with a directory you want to save graphs in


* Creating the British part of Figure 1: Distribution of class identity over time
preserve
tabulate classid3, generate(var)
collapse (sum) var1 var2 var3, by(year)
gen p1 = var1 / (var1 +var2 +var3)
gen p2 = (var1+var2) / (var1 +var2 +var3)
gen p3 = (var1+var2+var3) / (var1 +var2 +var3)
gen p4 = 1
gen zero = 0

twoway rarea zero p1 year, color(gs0) || rarea p1 p2 year, color(gs14) || rarea p2 p3 year, color(gs8) legend(order(1 "Working class ID" 2 "No class ID" 3 "Middle class ID" )) ytitle(Percent) xla(1970 1974 1975 1979 1983 1987 1992 1997 2001 2003 2005 2006 2010 2012 2015 2017 2019, angle(45)) xtitle("") title("Britain") //saving(classdist3Britain.gph, replace)
restore

// graph use classdist3Britain.gph  
// graph export classdist3Britain.png, replace


* Creating the British part of Figure 2: Class identification and attitudes to redistribution 

* For the main paper: 2-class id, multivariate 
eststo redistBritain: reg redistP01 i.classid2##i.year i.agekat i.gender i.edu2 i.class
margins year#classid2
marginsplot, title("Britain") xtitle("") ytitle("Redistribution") recastci(rarea) xlabel(, angle(45)) ciopt(color(%20)) // saving(eclr2multiBritain.gph, replace)
// graph export eclr2multiBritain.png, replace


* Creating the British part of Figure 3: Class identification and attitudes to immigration

* For the main paper: 2-class id, multivariate 
eststo immigBritain: logit img i.classid2##i.year i.agekat i.gender i.edu2 i.class
margins year#classid2
marginsplot,  title("United Kingdom") xtitle("") ytitle("Immigration attitudes") recastci(rarea) xlabel(, angle(45)) ylabel(.3(.1).8) ciopt(color(%20)) // saving(img2multiBritain.gph, replace)
// graph export img2multiBritain.png, replace


* Creating the British part of Figure 6: Class id and vote choice

* For the main paper: Multivariate, 2 classes
eststo partyBritain: mlogit vote4 i.classid2##i.year i.agekat i.gender i.edu2 i.class

margins year#classid2, predict(outcome(1))
marginsplot, title("Britain: Labour") xtitle("") ytitle("% support Labour") xlabel(, angle(45)) recastci(rarea)legend(row(1)) ciopt(color(%20))  ylab(0(0.1).7) //saving(lab2multiBritain.gph, replace) 
//graph export lab2multiBritain.png, replace

margins year#classid2, predict(outcome(2))
marginsplot, title("Britain: Conservative") xtitle("") ytitle("% support Cons") xlabel(, angle(45)) recastci(rarea) ciopt(color(%20)) ylab(0(0.1).7) // saving(cons2multiBritain.gph, replace) 
//graph export cons2multiBritain.png, replace



*** PART 2: ANALYSES OF PARTY POLARIZATION INTERACTION ***

use "C:\Users\\`=c(username)'\Dropbox\02_ClassIDscandi\Data\BES\replicationfile_Britain.dta", clear // replace with your directory

merge m:1 year using "C:\Users\\`=c(username)'\Dropbox\02_ClassIDscandi\Data\BES\UKmanifest.dta" // replace with your directory
drop if _merge<3

xtset year

recode vote4 (2=0) (3 4=.), gen(party2)
lab def party2 0 Right 1 Left
lab val party2 party2

gen ecopos= ecoposuk
gen autlib= autlibuk
gen ecopos2= ecoposuk2
gen autlib2= autlibuk2


** Replicating the British numbers in Table 2

* MI
eststo m1uk: xtmlogit party2 i.classid2##c.ecopos i.agekat i.gender i.edu2 i.class if vote4<3, re base(1)
//estimates save "C:\Users\au22016\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Data\m1uk", replace
test 1.classid2#ecopos
* MII
eststo m2uk: xtmlogit party2 i.classid2##c.autlib i.agekat i.gender i.edu2 i.class if vote4<3, re base(1)
//estimates save "C:\Users\au22016\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Data\m2uk", replace
test 1.classid2#autlib
* MIII
eststo m3uk: xtmlogit party2 i.classid2##c.ecopos i.classid2##c.autlib i.agekat i.gender i.edu2 i.class if vote4<3, re base(1)
//estimates save "C:\Users\au22016\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Data\m3uk", replace
test 1.classid2#ecopos
test 1.classid2#autlib

reg party2 i.classid2 c.ecopos c.autlib i.agekat i.gender i.edu2 i.class if vote4<3, r
estat vif

